﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace ZuesControls.Controls
{
   
    public class ZSExpander : Expander
    {
      
      
        private static ZSExpander Expander;
        private static Border _border;
        public static Grid grid;
        public ToggleButton _button;
        public ContentPresenter contentPresenter;
        public bool IsCheckedEvent
        {
            get { return (bool)GetValue(IsCheckedEventProperty); }
            set { SetValue(IsCheckedEventProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsCheckedEvent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsCheckedEventProperty =
            DependencyProperty.Register("IsCheckedEvent", typeof(bool), typeof(ZSExpander), new PropertyMetadata(false, OnIsCheckedEventAttachedChanged));


        public static readonly RoutedEvent ToggleButtonCheckedEvent = EventManager.RegisterRoutedEvent(
              "ToggleButtonChecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Window));

        // 定义 ToggleButtonUnchecked 路由事件
        public static readonly RoutedEvent ToggleButtonUncheckedEvent = EventManager.RegisterRoutedEvent(
            "ToggleButtonUnchecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Window));

        // 为 ToggleButtonChecked 事件添加 CLR 事件包装器
        public event RoutedEventHandler ToggleButtonChecked
        {
            add { AddHandler(ToggleButtonCheckedEvent, value); }
            remove { RemoveHandler(ToggleButtonCheckedEvent, value); }
        }

        // 为 ToggleButtonUnchecked 事件添加 CLR 事件包装器
        public event RoutedEventHandler ToggleButtonUnchecked
        {
            add { AddHandler(ToggleButtonUncheckedEvent, value); }
            remove { RemoveHandler(ToggleButtonUncheckedEvent, value); }
        }

        private static void OnIsCheckedEventAttachedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          
        }

        #region 内容板颜色
        public Brush HeaderBackground
        {
            get => (Brush)GetValue(HeaderBackgroundProperty);
            set => SetValue(HeaderBackgroundProperty, value);
        }

        public static readonly DependencyProperty HeaderBackgroundProperty =
            DependencyProperty.Register(
                nameof(HeaderBackground),
                typeof(Brush),
                typeof(ZSExpander),
                new PropertyMetadata(Brushes.Transparent));
        #endregion
     
        private static void OnToggleButtonUnchecked(object sender, RoutedEventArgs e)
        {
            
        }

        private static void OnToggleButtonChecked(object sender, RoutedEventArgs e)
        {
           
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            //   _toggleButton = this.GetTemplateChild("PART_BtnTest") as ToggleButton;
            _button = this.GetTemplateChild("PART_ToggleButton") as ToggleButton;
            contentPresenter = this.GetTemplateChild("contentPresenter") as ContentPresenter;
            //_button.Checked += _button_Checked;
            //_button.Unchecked += _button_Unchecked;
            
        }

        private void _button_Unchecked(object sender, RoutedEventArgs e)
        {
           
            if (contentPresenter == null) return;
            contentPresenter.Visibility = Visibility.Visible;
            var sb = new Storyboard();
            // 淡出动画
            var opacityAnim = new DoubleAnimation
            {
                From = 1,
                To = 0,
                Duration = TimeSpan.FromMilliseconds(320)
            };
            Storyboard.SetTarget(opacityAnim, contentPresenter);
            Storyboard.SetTargetProperty(opacityAnim, new PropertyPath("Opacity"));
            sb.Children.Add(opacityAnim);
            // 缩回动画
            var scaleYAnim = new DoubleAnimation
            {
                From = 1,
                To = 0,
                Duration = TimeSpan.FromMilliseconds(320),
                AccelerationRatio = 0.8,
                DecelerationRatio = 0.2
            };
            // 获取 ScaleTransform
            var scale = contentPresenter.LayoutTransform as ScaleTransform;
            if (scale == null)
            {
                scale = new ScaleTransform(1, 1);
                contentPresenter.LayoutTransform = scale;
            }

            Storyboard.SetTarget(scaleYAnim, contentPresenter);
            Storyboard.SetTargetProperty(scaleYAnim, new PropertyPath("LayoutTransform.ScaleY"));
            sb.Children.Add(scaleYAnim);
            // 动画结束后隐藏
            sb.Completed += (s, _) =>
            {
                contentPresenter.Visibility = Visibility.Collapsed;
            };

            sb.Begin();
            // 触发 ToggleButtonUnchecked 路由事件
            RoutedEventArgs newEventArgs = new RoutedEventArgs(ToggleButtonUncheckedEvent);
            RaiseEvent(newEventArgs);
        }

        private void _button_Checked(object sender, RoutedEventArgs e)
        {
           

            if (contentPresenter == null) return;

            contentPresenter.Visibility = Visibility.Visible;

            var sb = new Storyboard();

            var opacityAnim = new DoubleAnimation
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromMilliseconds(300)
            };
            Storyboard.SetTarget(opacityAnim, contentPresenter);
            Storyboard.SetTargetProperty(opacityAnim, new PropertyPath("Opacity"));
            sb.Children.Add(opacityAnim);

            var scaleYAnim = new DoubleAnimation
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromMilliseconds(300),
                AccelerationRatio = 0.2,
                DecelerationRatio = 0.8
            };

            if (contentPresenter.LayoutTransform is not ScaleTransform scale)
            {
                scale = new ScaleTransform(1, 0);
                contentPresenter.LayoutTransform = scale;
            }

            Storyboard.SetTarget(scaleYAnim, contentPresenter);
            Storyboard.SetTargetProperty(scaleYAnim, new PropertyPath("LayoutTransform.ScaleY"));
            sb.Children.Add(scaleYAnim);
          
            sb.Begin();
            RoutedEventArgs newEventArgs = new RoutedEventArgs(ToggleButtonCheckedEvent);
            RaiseEvent(newEventArgs);//把事件冒泡出去
        }

        static ZSExpander()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSExpander), new FrameworkPropertyMetadata(typeof(ZSExpander)));
           
        }
      
        public static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child != null && child is T typedChild)
                {
                    return typedChild;
                }
                else
                {
                    T foundChild = FindVisualChild<T>(child);
                    if (foundChild != null)
                        return foundChild;
                }
            }
            return null;
        }

    
        private static Grid FindInnerGrid(ZSExpander expander)
        {
            return expander.Content as Grid;
        }
    }
}
